package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Customer;

public class RegCustomer extends JPanel{
	
	private static final long serialVersionUID = 4241575775048921724L;

	private Customer cust;
	private JTextField cidText, pText, nText, aText, phText;
	private JTextArea textArea;
	private JScrollPane confirmation;
	private JButton addCustButton;
	
	// main panel where components are added
	public RegCustomer(Customer cust) {
		
		this.cust = cust;
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		inputFieldsPanel();
		
		buttonPanel();
		
		confirmPanel();
	}
	
	// jtextareas for input
	private void inputFieldsPanel() {
		JPanel textFieldPanel = new JPanel(new GridLayout(5,2));
		textFieldPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		textFieldPanel.add(new JLabel("Customer ID: "));
        cidText = new JTextField();
        cidText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(cidText);

        textFieldPanel.add(new JLabel("Password: "));
        pText = new JTextField();
        pText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(pText);
        
        textFieldPanel.add(new JLabel("Name: "));
        nText = new JTextField();
        nText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(nText);
        
        textFieldPanel.add(new JLabel("Address:"));
        aText = new JTextField();
        aText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(aText);
        
        textFieldPanel.add(new JLabel("Phone: (ie. 000-000-0000)"));
        phText = new JTextField();
        phText.setPreferredSize(new Dimension(300, 30));
        textFieldPanel.add(phText);
        
        add(textFieldPanel);
	}

	// button for adding customers
	private void buttonPanel() {
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		addCustButton = new JButton("Register");
		addCustButton.addActionListener(new addCustomer());
        buttonPanel.add(addCustButton, BorderLayout.CENTER);
		
		add(buttonPanel);
	}
	
	// jtextarea used for successful registrations
	private void confirmPanel() {
		JPanel conPanel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Registration Confirmation");
		conPanel.setBorder(title);
		
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        
        confirmation = new JScrollPane(textArea);
        confirmation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        conPanel.add(confirmation, BorderLayout.CENTER);
		
        add(conPanel);
	}
	
	// listener for button
	private class addCustomer implements ActionListener{
		
		String cid, pword, nam, adrss, ph;
		
		public void actionPerformed(ActionEvent e) {
			// get the text field data
			cid = cidText.getText();
			pword = pText.getText();
			nam = nText.getText();
			adrss = aText.getText();
			ph = phText.getText();
			
			
			// if any of the fields are not filled in
			if (cid.isEmpty() || pword.isEmpty() || nam.isEmpty() || adrss.isEmpty()) {
				JOptionPane.showMessageDialog(addCustButton,
					    "Please check that the following have been entered:\n- Customer ID\n- Password\n- Name\n- Address",
					    "Registration Error",
					    JOptionPane.ERROR_MESSAGE);
			} else { 
				try {
					boolean success = false;
					
						success = cust.newCustomer(cid, pword, nam, adrss, ph);
					
					// if the tuple is not added
					if (success == false) {
						JOptionPane.showMessageDialog(addCustButton,
							    "Customer Registration Failed.\nPlease check that all fields are properly filled in.",
							    "Registration Error",
							    JOptionPane.ERROR_MESSAGE);
					} else {
						//add information in the confirmation window
						textArea.append("******************* Customer Added ******************" + "\n" +
										"\n" + "Customer ID: " + cid + 
										"\n" + "Password: " + pword + 
										"\n" + "Name: " + nam + 
										"\n" + "Address: " + adrss + 
										"\n" + "Phone: " + ph +
										"\n\n" + 
										"*********************************************************" + "\n\n");
					}
					
				// if the primary key is violated (same cid)
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(addCustButton,
						    "The Customer ID has been already been taken.\nPlease enter a new Customer ID.",
						    "Registration Error",
						    JOptionPane.ERROR_MESSAGE);
					System.out.println("A problem occurred while registering.");
				}
			}
	    }
	}
}
